package com.eveinfo.tools;

import android.content.Context;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.eveinfo.R;

public class MyGestureDetector extends SimpleOnGestureListener {
	
	private static final int SWIPE_MIN_DISTANCE = 70;
	private static final int SWIPE_THRESHOLD_VELOCITY = 150;

	private final Context context;
	private final ViewFlipper viewFlipper;
	
	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
	private Animation slideRightOut;
	

	public MyGestureDetector(Context context, ViewFlipper viewFlipper) {
		this.context = context;
		this.viewFlipper = viewFlipper;
	}
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.push_left_in);
		slideLeftOut = AnimationUtils.loadAnimation(context, R.anim.push_left_out);
		slideRightIn = AnimationUtils.loadAnimation(context, R.anim.push_right_in);
		slideRightOut = AnimationUtils.loadAnimation(context, R.anim.push_right_out);
		try {
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				viewFlipper.setInAnimation(slideLeftIn);
				viewFlipper.setOutAnimation(slideLeftOut);
				viewFlipper.showNext();
			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				viewFlipper.setInAnimation(slideRightIn);
				viewFlipper.setOutAnimation(slideRightOut);
				viewFlipper.showPrevious();
			}
		} catch (Exception e) {
			// nothing
		}
		return false;
	}
}
